package syf.sparsearray;

import java.io.*;

public class review {
    public static void main(String[] args) {
        /*  输出文件的时候注在write后加一个"\t" 不然输出不正确不知道为啥

        * */
        int[][] chessArr=new int[11][11];
        chessArr[1][2]=1;
        chessArr[2][3]=2;
        //原本的棋盘
        for (int row1=0;row1<chessArr.length;row1++){
            for (int col1=0;col1<chessArr.length;col1++)
                System.out.print(chessArr[row1][col1]+"\t");
            System.out.println();
        }
        int sum=0;
        for (int row1=0;row1<chessArr.length;row1++){
            for (int col1=0;col1<chessArr.length;col1++) {
               if (0!=chessArr[row1][col1])
                   sum++;
            }
        }
        int[][] sparseArr=new int[sum+1][sum+1];
        sparseArr[0][0]=chessArr.length;
        sparseArr[0][1]=chessArr.length;
        sparseArr[0][2]=sum;
        int count=1;
        for (int row1=0;row1<chessArr.length;row1++){
            for (int col1=0;col1<chessArr.length;col1++) {
                if (0!=chessArr[row1][col1]) {
                    sparseArr[count][0]=row1;
                    sparseArr[count][1]=col1;
                    sparseArr[count++][2]=chessArr[row1][col1];
                }
            }
        }
        ///写文件
        BufferedWriter file=null;
        try {
            file=new BufferedWriter(new FileWriter("save.txt"));
            for (int cnt=0;cnt<sparseArr.length;cnt++) {
                for (int cnt1=0;cnt1<sparseArr.length;cnt1++){
                    file.write(sparseArr[cnt][cnt1]+"\t");
                }
                file.newLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (file!=null) {
                try {
                    file.flush();
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("===============");
//读文件
        BufferedReader bw=null;
        String date;
        try {
            bw=new BufferedReader(new FileReader("save.txt"));
            while ((date=bw.readLine())!=null)
                System.out.println(date+"\t");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (bw!=null)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("=============");

        for (int row1=0;row1<sparseArr.length;row1++){
            for (int col1=0;col1<sparseArr.length;col1++)
                System.out.print(sparseArr[row1][col1]+"\t");
            System.out.println();
        }
        int[][]oldArr=new int[sparseArr[0][0]][sparseArr[0][1]];
        for (--count;count>=1;--count){
            oldArr[sparseArr[count][0]][sparseArr[count][1]]=sparseArr[count][2];
        }
        for (int row1=0;row1<oldArr.length;row1++){
            for (int col1=0;col1<oldArr.length;col1++)
                System.out.print(oldArr[row1][col1]+"\t");
            System.out.println();
        }
    }
}
